package leetcode;

import java.util.HashMap;

public class sixteen {
    public static String minWindow(String s, String t) {
        int m=s.length();
        int n=t.length();
        if(m<n){
            return "";
        }
        HashMap<Character,Integer> hashMap=new HashMap<>();
        for(int i=0;i<n;i++){
            char in=t.charAt(i);
            hashMap.put(in,hashMap.getOrDefault(in,0)+1);
        }
        int minlength=Integer.MAX_VALUE;
        String minWindow="";
        for(int j=0;j<m;j++){
            HashMap<Character,Integer> hashMap1=new HashMap<>();
            for(int i=j;i<m;i++){
                char c=s.charAt(i);
                hashMap1.put(c,hashMap1.getOrDefault(c,0)+1);
                boolean containsAll=true;
                //遍历hashMap中所有的键值对
                for(char c1: hashMap.keySet()){
                    if(hashMap1.getOrDefault(c1,0)<hashMap.get(c1)){
                        containsAll=false;
                        break;
                    }
                }
                //如果窗口更小，更新
                if(containsAll&&(i-j+1)<minlength){
                    minlength=i-j+1;
                    minWindow=s.substring(j,i+1);

                }
                if(containsAll){
                    break;
                }


            }
        }
        return minWindow;
    }

    public static void main(String[] args) {
//        s = "ADOBECODEBANC", t = "ABC"
//        s = "aabbcc"
//        t = "abc"
//        s = "xyyzyzyx"
//        t = "xyz"
        String s="ADOBECODEBANC";
        String t="ABC";
        String s1 = minWindow(s, t);
        System.out.println(s1);
    }
}
